package com.project.sianet.data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.project.sianet.R;
import com.project.sianet.entidades.*;
import com.project.sianet.util.TypefaceManager;

public class NoticiaArrayAdapter extends ArrayAdapter<Noticia> {

	private static final String tag = "NoticiaArrayAdapter";
    private Context context;
    private TextView noticiaFecha;
    private TextView noticiaTitulo;
    private List<Noticia> noticias = new ArrayList<Noticia>();
    private int mCount = 5;
    SimpleDateFormat isoFormat;
    
    public NoticiaArrayAdapter(Context context, int textViewResourceId,
            List<Noticia> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.noticias = objects;
        
        isoFormat = new SimpleDateFormat("dd-MMM");
//        isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    }
    
    public int getCount() {
    	return this.noticias.size();
    }
    
    public int getCurrentCount() {
    	return mCount;
    }
 
    public Noticia getItem(int index) {
        return this.noticias.get(index);
    }
    
    public void addMoreItems(int count) {
        mCount += count;
        notifyDataSetChanged();
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            // ROW INFLATION
            Log.d(tag, "Starting XML Row Inflation ... ");
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.noticias_list, parent, false);
            Log.d(tag, "Successfully completed XML Row Inflation!");
        }
 
        Noticia not = getItem(position);

        noticiaFecha = (TextView) row.findViewById(R.id.tv_fecha);
        noticiaTitulo = (TextView) row.findViewById(R.id.tv_titulo);
        noticiaTitulo.setTextAppearance(row.getContext(), R.style.Widget_TextView);
        noticiaTitulo.setTypeface(TypefaceManager.getInstance().getTypeface("MavenPro"), Typeface.NORMAL);
        
        String fecha = isoFormat.format(not.getNotFecha());
        noticiaFecha.setText(fecha.replace("-", Html.fromHtml("<br />")));
        
        if (!not.notNoLeida)
        	noticiaFecha.setBackgroundResource(R.drawable.tv_noticias_leida);
        else
        	noticiaFecha.setBackgroundResource(R.drawable.tv_noticias);

//        noticiaFecha.setText((String) android.text.format.DateFormat.format("dd", fecha) + Html.fromHtml("<br />") + (String) android.text.format.DateFormat.format("MMM", fecha));
        noticiaTitulo.setText(not.getNotTitulo());

        return row;
    }
	
}
